<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AppTopService
 *
 * @author Administrator
 */
import('@.Service.ConfigService');
import('@.Service.AppService');

class AppTopService {

    //自动计算排名200
    function releaseTop() {
        $appTopHistoryTable = D('app_top_history');
        $appTopTable = D('app_top');

        $appService = new AppService();
        $configService = new ConfigService();

        $topLastDate = $configService->getConfigValue('top_date');

        $topVal = $configService->getConfigValue('top');
        if (date('Y-m-d') == $topLastDate && $topVal == 1) {
            //今日排行榜已排好
            return false;
        }

        //$appTopTable->startTrans();
        //清空History
        $appTopHistoryTable->execute('delete from app_top_history');


        //复制到History表
        $topAll = $appTopTable->field('app_id, download_num, ranking')->select();

        $appTopHistoryTable->addAll($topAll);


        $appTop200 = $appService->listAppByDownLoadNum(200);

        //清空Top表
        $appTopTable->execute('delete from app_top');


        $topCurr = array();
        $index = 1;
        foreach ($appTop200 as $app) {
            array_push($topCurr, array('app_id' => $app['id'], 'download_num' => $app['download_num'], 'ranking' => $index++));
        }
        //添加新数据至AppTop表
        $appTopTable->addAll($topCurr);

        $appTopHistory = $appTopHistoryTable->field('app_id')->order('download_num desc')->select();

        $appTopToday = $appTopTable->field('app_id')->order('download_num desc')->select();

        //比较新旧排名顺序
        foreach ($appTopHistory as $key => $value) {
            $todayKey = array_search($value, $appTopToday);
            $ranking = 0;
            if (is_bool($todayKey) && !$todayKey) {
                $ranking = 200 - $key;
            } else {
                $ranking = $key - $todayKey;
            }
            $appCurr = $appTopToday[$key];
            $appCurr = $this->getAppTopByAppId($appCurr['app_id']);
            $appCurr['order_ranking'] = $ranking;
            $appTopTable->save($appCurr);
        }


        //更新标识
        $configService->setConfigValue('top_date', date('Y-m-d'));
        $configService->setConfigValue('top', 1);
        //$appTopHistoryTable->commit();

        return true;
    }

    function getAppTopByAppId($appId) {
        return D('app_top')->where(array('app_id' => $appId))->find();
    }

    function listAppTop($offset, $limit) {
        $result = S('AppTopService/listAppTop/' . $offset);

//        if (!$result) {
        $appTopTable = D('app_top');
        $result = $appTopTable
                ->join('app a on app_top.app_id = a.id')
                ->field('a.id, a.app_icon, a.name, a.tag, a.price, a.date, app_top.ranking, app_top.order_ranking')
                ->order('app_top.ranking asc')
                ->limit($offset . ',' . $limit)
                ->select();
        S('AppTopService/listAppTop/' . $offset, $result, ConstantUtil::$CACHE_TIME);
//        }
        return $result;
    }

    function updateAppTop($appTop) {
        $appTopTable = D('app_top');

        $appTopTable->save($appTop);
    }

    function insert($appTop) {
        $lastId = D('app_top')->add($appTop);

        return $lastId;
    }

    function insertAll($appTop) {
        
    }

}

?>
